#include"struct.h"
#include<stdio.h>
#include<string.h>
#include<time.h>

//按照编号顺序打印所有某类主治病症类别的药品信息
void search_curetype(pdatatype data2)
{
	printf("主治病症：\n");
	char curetype1[10];
	scanf("%s",curetype1);
	int a=1;
	while (data2!=NULL)
	{

		if (strcmp(data2->CureType,curetype1)==0){
			a=0;
			printf("药品名称:%s\n编号:%s\n生产日期:%s\n有效期:%s\n失效日期:%s\n主治病症类别:%s\n用法:%s\n一天%s次\n一次%s\n",
				   data2->name,data2->Number,data2->date.shengchan,data2->date.youxiaoqi,data2->date.shixiao,data2
				   ->CureType,data2->usage.UseType,data2->usage.OneDay,data2->usage.OneTime);
			break;
		}

		data2=data2->Next;
	}
	if(a)
		printf("没有！");


}



void compute(pdatatype data3){
	time_t nowtime;


	while(data3->Next!=NULL){
		nowtime=time(NULL);
		char expiry_date[9];
		int  mouth2,mouth3,pass_year,tmp1;
		int currenttime=1970+nowtime/60/60/24/365;
		if(strcmp(data3->date.shixiao,"#")==0){
			mouth2=(data3->date.shengchan[4]-'0')*10+(data3->date.shengchan[5]-'0')+\
					((data3->date.youxiaoqi[0])-'0')*10+(data3->date.youxiaoqi[1]-'0');

			pass_year=mouth2/12;

			mouth3=mouth2%12;

			expiry_date[0]=data3->date.shengchan[0];
			expiry_date[1]=data3->date.shengchan[1];
			expiry_date[6]=data3->date.shengchan[6];
			expiry_date[7]=data3->date.shengchan[7];

			tmp1=((data3->date.shengchan[2])-'0')*10+((data3->date.shengchan[3])-'0')+pass_year;


			expiry_date[2]=(tmp1/10)+'0';
			expiry_date[3]=(tmp1%10)+'0';
			expiry_date[4]=(mouth3/10)+'0';
			expiry_date[5]=(mouth3%10)+'0';
			expiry_date[8]='\0';
			strcpy(data3->date.shixiao,expiry_date);

		}


		currenttime%=100;
		mouth2=(data3->date.shixiao[2]-'0')*10+(data3->date.shixiao[3]-'0');
		if(currenttime>mouth2){
			data3->date.guoqi=1;
		}
		else if(currenttime==mouth2){
			nowtime=(nowtime/60/60/24)/30%12;
			if(nowtime-mouth2<=4&&(nowtime-mouth2>0))
				data3->date.soonguoqi=1;
			else
				if(nowtime-mouth2<=0)
					data3->date.guoqi=1;
				else
					printf("can't compute %s",data3->name);
		}

		data3=data3->Next;
	}





}

//【计算1】能够计算某种药品（按照编号或名称检索）的当前失效日期
void expiry_date_caculation(pdatatype data3)
{
	compute(data3);
	char number1[20];
	printf("请输入编号或名称\n");
	scanf("%s",number1);
	while (data3!= NULL)
	{
		if ((strcmp(data3->name,number1)==0)||(strcmp(data3->Number,number1)==0))
			break;

		data3=data3->Next;
	}
	if (data3==NULL)
		printf("不存在该药品\n");
	else
	{

		printf("%s\n",data3->date.shixiao);
	}
}






//【计算2】能够计算某种药品（按照编号或名称检索）的一日用量。
void amount_caculation(pdatatype data1)
{
	char number1[20];
	printf("请输入编号或名称\n");
	scanf("%s",number1);
	while (data1!=NULL)
	{
		if ((strcmp(data1->name,number1)==0)||(strcmp(data1->Number,number1)==0))
			break;

		data1=data1->Next;
	}



	if (data1==NULL)
		printf("不存在该药品\n");

	else
	{
		int i=0,j=0,amount;
		char max1,max2;

		max1=data1->usage.OneDay[0];
		for(i=0;i<=19;i++)
		{
			if ( data1->usage.OneDay[i]>max1)
				max1=data1->usage.OneDay[i];
		}


		max2=data1->usage.OneTime[0];
		for(j=0;j<=19;j++)
		{
			if ( data1->usage.OneTime[j]>max2)
				max2= data1->usage.OneTime[j];
		}
		amount=(max1-'0')*(max2-'0');

		printf("每日用量为:%d\n",amount);
	}
}
